package com.aaa.dang.common.systemlog.aop;

import com.aaa.dang.common.entity.OperationLog;
import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.SessionUtil;
import com.aaa.dang.modules.login.service.ILoginService;
import com.aaa.dang.modules.system.service.IOperationLogService;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * 系统日志切面类
 *
 * @author Dadazhao
 * @date 2018/12/26
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    private ILoginService loginService;
    @Autowired
    private IOperationLogService operationLogService;
    /**
     *  切入点
     */
    @Pointcut("@annotation(com.aaa.dang.common.systemlog.annotation.SysLog)")
    public void logPointCut(){
    }

    /**
     * 后置提醒
     * @param joinPoint
     */
    @AfterReturning("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint){
        System.out.println("进入切面-----------");
        //操作日志实例
        OperationLog operationLog = new OperationLog();
        //从切入点通过反射机制获得切入点处的方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("方法"+methodSignature);
        //得到方法
        Method method = methodSignature.getMethod();
        //通过方法获得注解
        SysLog sysLog = method.getAnnotation(SysLog.class);
        if (sysLog!=null){
            //操作类型
            String operateType = String.valueOf(sysLog.operateType());
            //操作描述
            String desc = String.valueOf(sysLog.desc());
            //添加到日志
            operationLog.setOperateType(operateType);
            operationLog.setDescription(desc);
        }
        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //得到请求的方法名
        String methodName = method.getName();
        //添加到日志
        operationLog.setMethod(className+"."+methodName);
        //获取用户名
        String employeeName = loginService.getEmployeeName();
        //判断是否登录成功
        if (employeeName==null || "null".equals(employeeName)){
            return;
        }
        //添加到日志
        operationLog.setUsername(employeeName);
        //得到请求的参数
        Object[] args = joinPoint.getArgs();
        String argsJson = JSON.toJSONString(args);
        //添加到日志
        operationLog.setParams(argsJson);
        //设置日志日期
        operationLog.setCreateDate(new Date());
        //获得请求ip
        String ip = SessionUtil.getIP();
        operationLog.setIp(ip);
        //持久化日志
        operationLogService.addOperationLog(operationLog);
    }

    /**
     * 异常业务捕获
     * @param joinPoint
     */
   /* @AfterThrowing(pointcut = "logPointCut()")
    public void saveErrorLog(JoinPoint joinPoint){
        System.out.println("进入异常切面-----------");
        //操作日志实例
        OperationLog operationLog = new OperationLog();
        //从切入点通过反射机制获得切入点处的方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        System.out.println("方法"+methodSignature);
        //得到方法
        Method method = methodSignature.getMethod();
        //通过方法获得注解
        SysLog sysLog = method.getAnnotation(SysLog.class);
        if (sysLog!=null){
            //操作类型
            String operateType = String.valueOf(sysLog.operateType());
            //操作描述
            String desc = String.valueOf(sysLog.desc());
            //添加到日志
            operationLog.setOperateType("ERROR");
            operationLog.setDescription(desc);
        }
        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //得到请求的方法名
        String methodName = method.getName();
        //添加到日志
        operationLog.setMethod(className+"."+methodName);
        //获取用户名
        String employeeName = loginService.getEmployeeName();
        //判断是否登录成功
        if (employeeName==null || "null".equals(employeeName)){
            return;
        }
        //添加到日志
        operationLog.setUsername(employeeName);
        //得到请求的参数
        Object[] args = joinPoint.getArgs();
        String argsJson = JSON.toJSONString(args);
        //添加到日志
        operationLog.setParams(argsJson);
        //设置日志日期
        operationLog.setCreateDate(new Date());
        //获得请求ip
        String ip = SessionUtil.getIP();
        operationLog.setIp(ip);
        //持久化日志
        operationLogService.addOperationLog(operationLog);
    }*/
}
